<?php


namespace app\basic\controller;


use app\self\controller\Extend;
use think\Db;

class Picture   extends Extend
{
    protected $database = 'b_picture';

    protected $type = '图片管理';

    public function searchWhere()
    {
        $search = [
            ['field'=>'type','name'=>'分类','type'=>'select'],
        ];
        $this->assign('search',$search);
        $this->assign('searchJs',json_encode($search));
        $data['type'] = [
            ['value'=>'轮播图','key'=>'轮播图'],
            ['value'=>'背景图','key'=>'背景图'],
//            ['value'=>'技术知识','key'=>'技术知识'],

        ];
        $this->assign('selectData',$data);
    }

    public function mapSelf()
    {
        return ['is_delete'=>0];
    }



    public function edit()
    {
        $data['type'] = [
            ['value'=>'轮播图','key'=>'轮播图'],
            ['value'=>'背景图','key'=>'背景图'],
//            ['value'=>'技术知识','key'=>'技术知识'],

        ];
        $this->assign('selectData',$data);
        $id = input('id');
        $data = DB::name($this->database)
            ->where(['id'=>$id])
            ->find();
        $this->assign('data',$data);
        return view();
    }

    /**
     * use  保存数据
     * user  Hugh
     * date  2021/4/7 14:30
     */
    public function postData()
    {
        $tokenUser = input('tokenUser');
        if(trim($tokenUser,'"')  != $this->token) return clearLoginInfo();
        $data = input('data/a');
        $date = date('Y-m-d H:i:s');
        $data['update_time'] = $date;

        if(!empty($data['id']))
        {
            DB::name($this->database)
                ->where(['id'=>$data['id']])
                ->update($data);
            wlog($this->type,'图片更新',$data['id']);
        }
        else{
            $data['create_time'] = $date;
            DB::name($this->database)
                ->insert($data);
            wlog($this->type,'新增',serialize($data));
        }
        return success();
    }
}